package com.itheima.restkeeper.listener;

import com.itheima.restkeeper.constant.AppletCacheConstant;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.redisson.api.map.event.EntryEvent;
import org.redisson.api.map.event.EntryExpiredListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 购物车到期后处理事件
 * @Version: V1.0
 */
@Component
@Slf4j
public class ShopCartExpireListener implements EntryExpiredListener {

    @Autowired
    RedissonClient redissonClient;


    /**
     * 菜品到期后触发
     * @param event
     */
    @Override
    public void onExpired(EntryEvent event) {
        log.info("ShopCartExpireListener Dish increment start......");
        System.out.println(event.getKey());
        String keyDish = AppletCacheConstant.REPERTORY_DISH + event.getKey();
        RAtomicLong atomicLong = redissonClient.getAtomicLong(keyDish);
        //1、回滚菜品库存
        atomicLong.incrementAndGet();

        log.info("ShopCartExpireListener Dish increment success......");
    }
}
